﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Windows.Media.Imaging;
using System.Xml;

namespace TestAlbum.Class
{
    class Member
    {
        private string m_name;
        private string m_year;
        private string m_color;
        private string m_city;
        private List<string> m_pictureList = new List<string>();
        private List<string> m_modifications = new List<string>();
        private int m_rating;
        private int m_favorite;

        public string Name
        {
            get { return m_name; }
            set { m_name = value; }
        }

        public string Year
        {
            get { return m_year; }
            set { m_year = value; }
        }

        public string Color
        {
            get { return m_color; }
            set { m_color = value; }
        }

        public string City
        {
            get { return m_city; }
            set { m_city = value; }
        }

        public List<string> PictureList
        {
            get { return m_pictureList; }
            set { m_pictureList = value; }
        }

        public List<string> ModificationList
        {
            get { return m_modifications; }
            set { m_modifications = value; }
        }

        public int Rating
        {
            get { return m_rating; }
            set { m_rating = value; }
        }

        public int Favorite
        {
            get { return m_favorite; }
            set { m_favorite = value; }
        }

        public bool FavoriteBool
        {
            get { return Convert.ToBoolean(m_favorite); }
        }

        public BitmapImage FavoritePicture
        {
            get {
                if (m_favorite == 1)
                {
                    Uri src = new Uri("/TestAlbum;component/Ressources/onebit_43.png", UriKind.RelativeOrAbsolute);
                    return new BitmapImage(src);
                }
                else
                    return new BitmapImage();
            }
        }

        /// <summary>
        /// Return a string containing all the modifications, separated by a coma (,)
        /// </summary>
        public string ModificationStringFormatted
        {
            get
            {
                string modif = string.Empty;

                bool first = true;
                foreach (string s in m_modifications)
                {
                    if (!first)
                        modif += ", ";
                    else
                        first = false;

                    modif += s;
                }

                return modif;
            }
        }

        /// <summary>
        /// Return the first picture of the member
        /// Return null if none available
        /// </summary>
        public BitmapImage ImgToolTip1
        {
            get
            {
                if (m_pictureList.Count < 1)
                    return null;

                string sUri = System.Reflection.Assembly.GetExecutingAssembly().Location.Replace("TestAlbum.exe", "") + "\\" + m_pictureList[0];
                Uri src = new Uri(sUri, UriKind.RelativeOrAbsolute);
                return new BitmapImage(src);
            }
        }

        /// <summary>
        /// Return the first picture of the member
        /// Return null if none available
        /// </summary>
        public BitmapImage ImgToolTip2
        {
            get
            {
                if (m_pictureList.Count < 2)
                    return null;

                string sUri = System.Reflection.Assembly.GetExecutingAssembly().Location.Replace("TestAlbum.exe", "") + "\\" + m_pictureList[1];
                Uri src = new Uri(sUri, UriKind.RelativeOrAbsolute);
                return new BitmapImage(src);
            }
        }

        /// <summary>
        /// Return the first picture of the member
        /// Return null if none available
        /// </summary>
        public BitmapImage ImgToolTip3
        {
            get
            {
                if (m_pictureList.Count < 3)
                    return null;

                string sUri = System.Reflection.Assembly.GetExecutingAssembly().Location.Replace("TestAlbum.exe", "") + "\\" + m_pictureList[2];
                Uri src = new Uri(sUri, UriKind.RelativeOrAbsolute);
                return new BitmapImage(src);
            }
        }

        public void Save()
        {
            XElement members = XElement.Load(@"Data\database.xml");

            foreach (XElement m in members.Elements("memberList").Elements("member"))
            {
                if(m.Element("name").Value == m_name)
                {
                    m.Element("favorite").SetValue(m_favorite.ToString());
                    break;
                }
            }
            members.Save(@"Data\database.xml");
        }
    }

    class MemberList
    {
        public static List<Member> GetList()
        {
            //string xmlPath = "database.xml";
            //string picutrePath = @"\Pictures\";

            string xmlPath = Tools.GetDatabaseFile;
            string picutrePath = Tools.GetPicturePath;

            List<Member> m_list = new List<Member>();

            XElement members = XElement.Load(@"Data\database.xml");

            foreach (XElement m in members.Elements("memberList").Elements("member"))
            {
                Member memberToAdd = new Member();
                memberToAdd.Name = m.Element("name").Value;
                memberToAdd.Year = m.Element("year").Value;
                memberToAdd.Color = m.Element("color").Value;
                memberToAdd.City = m.Element("city").Value;

                foreach (XElement p in m.Elements("pictureList").Elements("picture"))
                {
                    memberToAdd.PictureList.Add(picutrePath + p.Value);
                }

                foreach (XElement mo in m.Elements("modifications").Elements("modification"))
                {
                    memberToAdd.ModificationList.Add(mo.Value);
                }

                try
                { memberToAdd.Rating = Convert.ToInt32(m.Element("rating").Value); }
                catch
                {  memberToAdd.Rating = 0; }

                try
                { memberToAdd.Favorite = Convert.ToInt32(m.Element("favorite").Value); }
                catch
                { memberToAdd.Favorite = 0; }

                m_list.Add(memberToAdd);
            }

            return m_list;
        }


    }
}
